package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.PageVO;
import com.huiminpay.common.cache.util.QRCodeUtil;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.dto.StoreDTO;
import com.huiminpay.transaction.api.TransactionService;
import com.huiminpay.transaction.dto.QRCodeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * ClassName:StoreController
 * Package:com.huiminpay.merchant.controller
 * Time: 2024/7/27
 */
@RestController
@Api(value = "商户平台‐门店管理", tags = "商户平台‐门店管理", description = "商户平台‐门店的增删改查")
@Slf4j
public class StoreController {
    @Reference
    MerchantService merchantService;

    @Reference
    TransactionService transactionService;

    @ApiOperation("分页条件查询商户下门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int",
                    paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType =
                    "int", paramType = "query")})
    @PostMapping("/my/stores/merchants/page")
    public PageVO<StoreDTO> queryStoreByPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        //1、获取商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setMerchantId(merchantId);
        //2、调用商户服务查询门店
        PageVO<StoreDTO> store = merchantService.queryStoreByPage(storeDTO, pageNo, pageSize);
        return store;
    }

    /**
     * 门店二维码订单标题
     */
    @Value("${huiminpay.c2b.subject}")
    private String subject;
    /**
     * 门店二维码订单内容
     */
    @Value("${huiminpay.c2b.body}")
    private String body;

    @ApiOperation(value = "创建C扫B商户门店二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "商户应用ID", required = true, dataType =
                    "String", paramType = "path"),
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, dataType =
                    "Long", paramType = "path")
    })
    @GetMapping("/my/apps/{appId}/stores/{storeId}/app-store-qrcode")
    public String createC2BStoreQRCode(@PathVariable String appId, @PathVariable Long storeId) {
        //1、获取商户ID
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = merchantService.MerchantById(merchantId);
        //2、调用交易服务创建C扫B商户门店二维码
        //生成二维码链接
        QRCodeDTO qrCodeDTO = new QRCodeDTO();
        qrCodeDTO.setMerchantId(merchantId);
        qrCodeDTO.setStoreId(storeId);
        qrCodeDTO.setAppId(appId);
        //设置订单标题,用商户名替换%s
        String subjectFormat = String.format(subject, merchantDTO.getMerchantName());
        qrCodeDTO.setSubject(subjectFormat);
        //设置订单内容
        String bodyFormat = String.format(body, merchantDTO.getMerchantName());
        qrCodeDTO.setBody(bodyFormat);
        //获取二维码URL
        String storeQRCodeUrl = transactionService.createStoreQRCode(qrCodeDTO);
        log.info("[merchantId:{},appId:{},storedId:{}]创建C扫B商户门店二维码成功,storeQRCodeUrl:{}",
                merchantId, appId, storeId, storeQRCodeUrl);

        try {
            //根据返回url，调用生成二维码工具类，生成二维码base64返回
            QRCodeUtil qrCodeUtil = new QRCodeUtil();
            String qrCode = qrCodeUtil.createQRCode(storeQRCodeUrl, 200, 200);
            return qrCode;
        } catch (IOException e) {
            throw new BusinessException(CommonErrorCode.E_200007); //二维码生成失败
        }
    }


}
